<template>
  <el-container class="tab-container">
    <el-aside width="auto" class="aside">
      <el-tree
        ref="tree"
        class="aside-tree"
        :data="treeData"
        node-key="id"
        :default-expanded-keys="cacheTreeExpandedKeys"
        @current-change="handleTreeSelectChange"
      />
    </el-aside>
    <el-main class="main">
      <collapse-tab class="main-search">
        <el-form
          :inline="true"
          :model="queryCondition"
          label-width="80px"
          @keyup.enter.native="query"
        >
          <!--查询条件区 -->
          <el-form-item label="姓名">
            <p-input v-model="queryCondition.name" class="form-item" />
          </el-form-item>
          <el-form-item label="账号">
            <p-input v-model="queryCondition.account" class="form-item" />
          </el-form-item>

          <el-form-item label="状态">
            <dictionary-select
              v-model="queryCondition.status"
              multiple
              :code="constant.DICTIONARY_TYPE_CODES.USER_STATUS"
            />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="queryCondition.ignoreParent">查询全部</el-checkbox>
          </el-form-item>
          <el-form-item style="float:right;">
            <list-button-query :page-code="pageCode" />
            <list-button-advance-query :page-code="pageCode" />
          </el-form-item>
          <div class="clearfix" />
        </el-form>
      </collapse-tab>
      <el-card class="main-report">
        <div style="width:100%">
          <list-button-refresh :page-code="pageCode" />
          <list-button-add :page-code="pageCode" />
          <list-button-remove :page-code="pageCode" />
          <list-button-export :page-code="pageCode" />
          <list-button-download-import-template :page-code="pageCode" />
          <list-button-import ref="listButtonImport" :page-code="pageCode" />

          <div style="clear:both;" />
        </div>
        <div style="margin-top:5px;">
          <div>
            <columns-controller v-model="columnList" :table-key="tableKey" />
          </div>
          <el-table
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            highlight-current-row
            border
            @sort-change="handleSortChange"
            @current-change="handleRowChange"
            @selection-change="handleSelectionChange"
            @row-dblclick="handleDoubleClickRow"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column
              v-for="(item, index) in showCols"
              :key="index"
              :label="item.label"
              :prop="item.prop"
              :show-overflow-tooltip="item.showOverflowTooltip"
              :width="item.width"
              :formatter="item.formatFunc"
              :sortable="item.sortable"
            />
            <el-table-column fixed="right" label="操作" width="200">
              <template slot-scope="scope">
                <table-button-modify :id="scope.row.id" :page-code="pageCode" />
                <el-button
                  v-permission="pageCode+'configUserGroup'"
                  type="text"
                  @click="configUserGroup(scope.row)"
                >用户组</el-button>
                <el-dropdown style="margin-left:10px;color:#1890ff">
                  <span class="el-dropdown-link">
                    更多
                    <i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>
                      <table-button-remove :id="scope.row.id" :page-code="pageCode" />
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <table-button-enable :id="scope.row.id" :page-code="pageCode" />
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <table-button-disable :id="scope.row.id" :page-code="pageCode" />
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-button
                        v-permission="pageCode+'resetPassword'"
                        type="text"
                        @click="resetPassword(scope.row.id)"
                      >重置密码</el-button>
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-button
                        v-permission="pageCode+'unlock'"
                        type="text"
                        @click="unlock(scope.row.id)"
                      >手工解锁</el-button>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <list-pager
          :page-num="pageInfo.pageNum"
          :page-size="pageInfo.pageSize"
          :page-total="pageTotal"
        />
      </el-card>
      <detail ref="detail" :parent-id="parentId" :parent-name="parentName" @ok="handleDetailSave" />
    </el-main>
    <advance-query-scheme ref="advanceQueryScheme" @advanceQuery="doAdvanceQuery" />
    <UserGroupMultipleSelectModal ref="userGroup" @ok="saveUserGroup" />
  </el-container>
</template>

<script>
import { listMixin } from '@/mixin/treeListMixin'
import Detail from './detail'

import AdvanceQueryScheme from '@/modules/support/view/advanceQueryScheme/list'
import UserGroupMultipleSelectModal from '@/components/popsoft/CommonSelect/UserGroup/Multiple/modal'
const MODULE_CODE = 'system'
const ENTITY_TYPE = 'user'
export default {
  name: ENTITY_TYPE,
  components: { Detail, AdvanceQueryScheme, UserGroupMultipleSelectModal },
  mixins: [listMixin],
  data() {
    return {
      entityType: ENTITY_TYPE,
      moduleCode: MODULE_CODE,
      // eslint-disable-next-line no-eval
      api: eval('this.$api.' + MODULE_CODE + '.' + ENTITY_TYPE),
      apiTree: this.$api.system.organization,
      pageCode: MODULE_CODE + ':' + ENTITY_TYPE + ':',
      queryCondition: {
        parentId: '',
        organizationId: ''
      },
      columnList: [
        // prop label show 必须定义， width/showOverflowTooltip/formatFunc/sortable 需要的话定义
        { prop: 'name', label: '姓名', show: true },
        { prop: 'account', label: '账号', show: true },
        { prop: 'genderName', label: '性别', show: true },
        { prop: 'position', label: '职务', show: true, showOverflowTooltip: true },
        { prop: 'organizationName', label: '组织机构', show: true, showOverflowTooltip: true },
        { prop: 'telephone', label: '手机号', show: true },
        { prop: 'statusName', label: '状态', show: true },
        { prop: 'orderNo', label: '排序号', show: true }
      ],
      treeData: null,
      cacheTreeExpandedKeys: [this.constant.DEFAULT_EXPANDED_ID],
      isRefreshTree: false,
      parentId: '',
      parentName: '',
      // 方案编码
      entityCode: 'system-user'
    }
  },
  watch: {
    parentId() {
      this.queryCondition.organizationId = this.parentId
    }
  },

  methods: {
    resetPassword(id) {
      this.$confirm('是否重置密码?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.api.resetPassword(id)
          .then(() => {
            this.refresh()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作取消'
        })
      })
    },
    unlock(id) {
      this.$confirm('是否解锁账号?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.api.unlock(id)
          .then(() => {
            this.refresh()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作取消'
        })
      })
    },
    configUserGroup(row) {
      this.currentId = row.id
      this.$api.system.user.getUserGroup(row.id)
        .then(res => {
          this.$refs.userGroup.show(res.data)
        })
    },
    // 保存用户与用户组对应
    saveUserGroup(userGroupIdList) {
      this.$api.system.user.saveUserGroup(this.currentId, { userGroupIdList })
    }

  }
}
</script>

  <style lang="scss" scoped>
.tab-container {
  $item-width: 200px;
  padding: 15px;
  .aside {
    min-width: 180px;
    background-color: white;
  }
  .main {
    .main-search {
      .form-item {
        width: $item-width;
      }
    }
    .main-report {
      margin-top: 10px;
    }
  }
}
</style>
